//
//  DYSimilarPicItemCell.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYSimilarPicItemCell.h"
#import <Masonry.h>
#import "UIColor+Additions.h"
#import "VLocalImageCache.h"
#import "FPPhotosTool.h"
#import <ReactiveObjC.h>

@interface DYSimilarPicItemCell ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) PHAsset *asset;
@property (nonatomic, strong) UIButton *bottomLeftBtn;
@property (nonatomic, strong) UILabel *bottomRightLabel;

@end

@implementation DYSimilarPicItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self configSubViews];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-8);
        make.top.mas_offset(8);
        
    }];
   
    [self.bottomLeftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(6);
        make.bottom.mas_offset(-8);
        make.width.height.mas_equalTo(16);
    }];
    [self.bottomRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-3);
        make.bottom.mas_offset(-8);
    }];
}

- (void)configSubViews{
    
    [self.contentView addSubview:self.imageView];
   
    [self.contentView addSubview:self.selectButton];
    [self.contentView bringSubviewToFront:self.selectButton];
    
    [self.contentView addSubview:self.bottomLeftBtn];
    [self.contentView bringSubviewToFront:self.bottomLeftBtn];
   
    [self.contentView addSubview:self.bottomRightLabel];
    [self.contentView bringSubviewToFront:self.bottomRightLabel];
}

- (UICollectionView *)getSelfSupView
{
    UICollectionView *container = (UICollectionView*)self.superview;
    while (container != nil && ![container isKindOfClass:[UICollectionView class]]) {
           container = (UICollectionView*)container.superview;
    }
    return container;
}

- (void)setAsset:(PHAsset *)asset{
    _asset = asset;
    UIImage *image =  [[VLocalImageCache shareCache].cache objectForKey:asset.localIdentifier];
    
    if (image) {
        self.imageView.image = image;
    }
    else{
        @weakify(self)
       PHCachingImageManager *imageManager = [FPPhotosTool shareFPPhotosTool].cacheImageManager;
        [imageManager requestImageForAsset:asset targetSize:[FPPhotosTool shareFPPhotosTool].coverImageSize contentMode:PHImageContentModeAspectFill options:[FPPhotosTool shareFPPhotosTool].coverImageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            @strongify(self)
            if (result) {
                self.imageView.image = result;
                [[VLocalImageCache shareCache].cache setObject:result forKey:asset.localIdentifier];
            }
        }];
    }
}

- (void)updateViewWithModel:(DYSimilarPicModel *)model
                 selectedArray:(NSArray *)selectedArray
{
    self.asset = model.asset;
        
    BOOL isSeletedAsset = [selectedArray containsObject:model];
    if (isSeletedAsset){
        self.selectButton.selected = YES;
        CALayer *layer = [[CALayer alloc] init];
        layer.frame = self.bounds;
        layer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85].CGColor;
        self.layer.mask = layer;
    }else{
        self.selectButton.selected = NO;
        self.layer.mask = nil;
    }
}

- (void)selectedImageComplete:(UIButton *)button {
    
    !self.selectPhotoCompleteBlock ?:self.selectPhotoCompleteBlock();
}

#pragma mark --- setter/getter

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds = YES;
    }
    return _imageView;
}
- (UIButton *)selectButton {
    if (!_selectButton) {
        _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectButton.titleLabel.font = [UIFont systemFontOfSize:14.f];
        _selectButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_selectButton setImage:[UIImage imageNamed:@"bk"] forState:UIControlStateNormal];
        [_selectButton setImage:[UIImage imageNamed:@"xz"] forState:UIControlStateSelected];
        [_selectButton addTarget:self action:@selector(selectedImageComplete:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}

- (UIButton *)bottomLeftBtn {
    if (!_bottomLeftBtn) {
        _bottomLeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _bottomLeftBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_bottomLeftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _bottomLeftBtn.backgroundColor = [UIColor clearColor];
        _bottomLeftBtn.layer.cornerRadius = 16/2;
        _bottomLeftBtn.layer.masksToBounds = YES;
//        [_bottomLeftBtn addTarget:self action:@selector(selectedImageComplete:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomLeftBtn;
}

- (UILabel *)bottomRightLabel{
    if (!_bottomRightLabel) {
        _bottomRightLabel = [[UILabel alloc] init];
        _bottomRightLabel.font = [UIFont systemFontOfSize:12];
        _bottomRightLabel.textColor = [UIColor blackColor];
        _bottomRightLabel.textAlignment = NSTextAlignmentRight;
        _bottomRightLabel.text = @"最佳照片";
        _bottomRightLabel.hidden = YES;
    }
    return _bottomRightLabel;
}




@end
